package com.imist.jdbc.servlet;

import com.imist.jdbc.bean.User;
import com.imist.jdbc.service.UserServices;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;

public class UserServlet extends HttpServlet {
    private UserServices userServices;
    @Override
    public void init() throws ServletException {
        super.init();
        userServices = new UserServices();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathName = req.getServletPath();
        if (Objects.equals("/userInfo.do",pathName)){
            req.getRequestDispatcher("/WEB-INF/views/biz/user.jsp").forward(req,resp);
        }else if (Objects.equals("/editUserPrompt.do",pathName)){
            //1.可以从session中获取
            //2.可扩展的使用方式 根据页面的ID从数据库中查询
            Long id = Long.valueOf(req.getParameter("id"));
            User user = userServices.getUserById(id);
            if (null != user){
                req.setAttribute("user",user);
                req.getRequestDispatcher("/WEB-INF/views/biz/edit_user.jsp").forward(req,resp);
            }else {
                req.getRequestDispatcher("/WEB-INF/views/biz/user.jsp").forward(req,resp);
            }
        }else if (Objects.equals("/editUser.do",pathName)){
            Long id = Long.valueOf(req.getParameter("id"));
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String realName = req.getParameter("realName");
            String birthday = req.getParameter("birthday");
            String phone = req.getParameter("phone");
            String address = req.getParameter("address");
            User user = new User();
            user.setId(id);
            user.setName(name);
            user.setPassword(password);
            user.setRealName(realName);
            try {
                user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
            } catch (ParseException e) {
                //这里注意date的类，不同的显示方式 Thu Oct 11 00:00:00 CST 2018  ： 2018-10-14
                System.out.println("格式化birthday失败");
                e.printStackTrace();
                user.setBirthday(null);
            }
            user.setPhone(phone);
            user.setAddress(address);
            boolean result = userServices.updateUser(user);
            if (result){
                req.getSession().setAttribute("user",user);
                req.setAttribute("user",user);
                req.getRequestDispatcher("/WEB-INF/views/biz/user.jsp").forward(req,resp);
            }else {
                req.getRequestDispatcher("/WEB-INF/views/error/404.jsp").forward(req,resp);
            }
        }else {
            req.getRequestDispatcher("/WEB-INF/views/error/404.jsp").forward(req,resp);
        }

    }
}
